{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from api_key import api_key\n",
    "from openai import OpenAI\n",
    "\n",
    "endpoint = \"https://conductor.arcee.ai/v1\"\n",
    "\n",
    "model = \"coder\"\n",
    "\n",
    "client = OpenAI(base_url=endpoint, api_key=api_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Markdown, clear_output, display\n",
    "\n",
    "\n",
    "def print_streaming_response(response):\n",
    "    num_tokens = 0\n",
    "    content = \"\"\n",
    "    for message in response:\n",
    "        if len(message.choices) > 0:\n",
    "            num_tokens += 1\n",
    "            chunk = message.choices[0].delta.content\n",
    "            if chunk:\n",
    "                content += chunk\n",
    "                clear_output(wait=True)\n",
    "                display(Markdown(content))\n",
    "\n",
    "    print(f\"\\n\\nNumber of tokens: {num_tokens}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = client.chat.completions.create(\n",
    "    model=model,\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"\"\"Explain the difference between logit-based distillation and hidden state distillation.\n",
    "       Show an example for both with Pytorch code, \n",
    "       with BERT-Large as the teacher model, and BERT-Base as the student model.\n",
    "       \"\"\",\n",
    "        }\n",
    "    ],\n",
    "    temperature=0.9,\n",
    "    stream=True,\n",
    "    max_tokens=16384,\n",
    ")\n",
    "\n",
    "print_streaming_response(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "code_example = \"\"\"\n",
    "def print_streaming_response(response):\n",
    "    num_tokens=0\n",
    "    for message in response:\n",
    "        if len(message.choices) > 0:\n",
    "            num_tokens+=1\n",
    "            print(message.choices[0].delta.content, end=\"\")\n",
    "    print(f\"\\n\\nNumber of tokens: {num_tokens}\")\n",
    "\"\"\"\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=model,\n",
    "    messages=[\n",
    "        {\n",
    "            \"role\": \"user\",\n",
    "            \"content\": f\"Improve the following code and make it more pythonic: {code_example}. Explain why your changes are an improvement.\",\n",
    "        }\n",
    "    ],\n",
    "    temperature=0.9,\n",
    "    stream=True,\n",
    "    max_tokens=2048,\n",
    ")\n",
    "\n",
    "print_streaming_response(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env-openai-client",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
